﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MYQQX
{
    public class CardConfig
    {
        public static CardConfig Instance { get; private set; } = new CardConfig();

        private List<Card> AllCards = new List<Card>();
        private CardConfig()
        {

        }

        public void Init()
        {
            var str = Properties.Resources.CardJson;
            JArray array = JArray.Parse(str);

            foreach (JArray jar in array)
            {
                foreach (var card in jar)
                {
                    var gv = int.Parse(card[0].ToString().Last().ToString());

                    AllCards.Add(new Card() { Name = card[1].ToString(), JiJie = GetSeason(card[3].ToString()), GameVersion = gv });
                }
            }
            var rand = new Random();
            for (int i = 0; i < AllCards.Count; i++)
            {
                var next = rand.Next(0, AllCards.Count);
                var card = AllCards[next];
                AllCards.RemoveAt(next);
                AllCards.Add(card);
            }
        }

        private CardSeason GetSeason(string s)
        {
            switch (s)
            {
                case "春":
                    return CardSeason.Chun;
                case "夏":
                    return CardSeason.Xia;
                case "秋":
                    return CardSeason.Qiu;
                case "东":
                default:
                    return CardSeason.Dong;
            }
        }

        public List<Card> GetCardList(bool h1, bool h2, bool h3)
        {
            var sum = (h1 ? 1 : 0)
                + (h2 ? 1 : 0)
                + (h3 ? 1 : 0);
            if (sum <= 1)
            {
                throw new Exception("至少选择两个！");
            }

            List<Card> list = new List<Card>();
            if (h1)
                list.AddRange(AllCards.Where(t => t.GameVersion == 1));
            if (h2)
                list.AddRange(AllCards.Where(t => t.GameVersion == 2));
            if (h3)
                list.AddRange(AllCards.Where(t => t.GameVersion == 3));

            return list;

        }

        public Card GetCardByName(string name)
        {
            return AllCards.FirstOrDefault(t => t.Name == name)?.Clone();
        }
    }
}
